from com.cqt.tools.mysql.table_fields import TableField, Table


class JavaModel:

    def __init__(self, table:Table):
        self.table = table
        self.keeps = [
            'createTime',
            'updateTime',
            'creator',
            'updater',
            'deleted',
            'tenantId', 'deleted',

            'updatedAt', 'createdAt', 'createdBy', 'updatedBy'
        ]

    def echoJavaFile(self
                     , javaName="",
                     packageName=""
                     ,
                     baseDir=r"D:\workspace\boss-platform\boss-platform-service\boss-platform-service-charge-crm\src\main\java",
                     ):
        firstLine = '''
package '''+packageName+''';

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.linkcircle.boss.framework.tanant.TenantBaseDO;
import lombok.*;
 /**
 * '''+self.table.tableComment+''' DO
 *
 * @author zengxiaoyu
 */
@TableName(value = "''' + self.table.tableName + '''", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ''' + javaName + """ extends TenantBaseDO {
        """
        middleLines = self.echoJava(isPrint=False)
        newPackageToDir = packageName.replace(".", "\\")
        file_path = '{}\\{}\\{}.java'.format(baseDir, newPackageToDir, javaName)
        print(file_path)
        with open(file=file_path, mode='w', encoding='utf-8') as f:
            f.write(firstLine);
            for line in middleLines:
                f.write(line)
            f.writelines("}")

    def echoJava(self, isPrint=True):
        lines = []
        for tf in self.table.tfs:
            mf = tf.to_map()

            if mf['javaName'] in self.keeps:
                continue
            if mf['isPrimary']:
                line = """
                      /**
                      {}
                      */
                      @TableId(type = IdType.ASSIGN_ID)
                      private {} {};
                                """.format(mf['remark'], mf['javaType'], mf['javaName']);
                lines.append(line)
                if isPrint:
                    print(line)

            else:
                # print(mf)
                line = """
                /**
                 {}
                */
                @TableField(value="{}")
                private {} {};
                """.format(mf['remark'], mf['name'], mf['javaType'], mf['javaName'])
                lines.append(line)
                if isPrint:
                    print(line)
        return lines
